<?php
/**
 * Created by PhpStorm.
 * User: silenceper
 * Date: 2016/12/13
 * Time: 下午5:21
 */

namespace mybackend\controllers;


use common\entities\myhtsq\ActionCode;
use common\entities\outstock\MyscrmOutStockOrderEntity;
use common\services\outstock\FlowCenterService;
use common\services\outstock\DeliveryOrderService;

class FlowCenterController extends BaseController
{

    private $_flowCenterService;

    public function __construct($id, $module, FlowCenterService $flowCenterService, $config = [])
    {
        $this->_flowCenterService = $flowCenterService;
        parent::__construct($id, $module, $config);
    }

    protected function roleAuthRules()
    {
        $data = [
            'List' => ActionCode::FLOW_CENTER_QUERY,
        ];

        return [
            'actions' => array_keys($data),
            'rules'   => [
                [
                    'allow'          => true,
                    'actionCodeMaps' => $data
                ],
            ],
        ];
    }

    /**
     * 流程中心列表
     * @param int $action
     * @param int $page
     * @param int $pageSize
     * @param string $keywords
     */
    public function actionList($action = 1, $page = 1, $pageSize = 10, $keywords = "")
    {
        $res = [];
        switch ($action) {
            case MyscrmOutStockOrderEntity::FLOW_START:
                $res = $this->_flowCenterService->getStartList($page, $pageSize, $keywords);
                break;
            case MyscrmOutStockOrderEntity::FLOW_TODO:
                $res = $this->_flowCenterService->getTodoList($page, $pageSize, $keywords);
                break;
            case MyscrmOutStockOrderEntity::FLOW_DONE:
                $res = $this->_flowCenterService->getDoneList($page, $pageSize, $keywords);
                break;
        }

        $this->exportJson($res);
    }

    /**
     * 根据code获取详情
     * @param string $order_code
     */
    public function actionGetDetailByCode($order_code = "")
    {
        $data = $this->_flowCenterService->getDetailByCode($order_code);
        $this->exportJson($data);
    }

    /**
     * 根据order_code获取授权产品
     * @param $order_code
     */
    public function actionGetAllPackages($order_code)
    {
        $res = $this->_flowCenterService->getAllPackages($order_code);
        $this->exportJson($res, $this->_flowCenterService->getErrCode(), $this->_flowCenterService->getErrMsg());
    }
    public function actionDeliveryOrderList()
    {
        $page=\Yii::$app->request->get("page",1);
        $page=max(1,$page);
        $pageSize=\Yii::$app->request->get("page-size",10);
        $type=\Yii::$app->request->get("type",0);
        $step=\Yii::$app->request->get("step",0);
        $keyword=\Yii::$app->request->get("keyword",'');
        /* @var DeliveryOrderService $deliveryOrderService */
        $deliveryOrderService=\Yii::createObject(DeliveryOrderService::className());
        $data=$deliveryOrderService->getDeliveryOrderList($page,$pageSize,$type,$step,$keyword);
        return $this->exportJson($data, $deliveryOrderService->getErrCode(), $deliveryOrderService->getErrMsg());
    }
    public function actionDeliveryTodoList()
    {
        $page=\Yii::$app->request->get("page",1);
        $page=max(1,$page);
        $pageSize=\Yii::$app->request->get("page-size",10);
        $type=\Yii::$app->request->get("type",0);
        $status=\Yii::$app->request->get("status",0);
        $keyword=\Yii::$app->request->get("keyword",'');
        $startTime=\Yii::$app->request->get("delivery_start_time",0);
        $endTime=\Yii::$app->request->get("delivery_end_time",0);
        $timeQuantum=['startTime'=>$startTime,'endTime'=>$endTime];
        /* @var DeliveryOrderService $deliveryOrderService */
        $deliveryOrderService=\Yii::createObject(DeliveryOrderService::className());
        $data=$deliveryOrderService->getDeliveryTodoList($page,$pageSize,$type,$status,$keyword,$timeQuantum);
        return $this->exportJson($data, $deliveryOrderService->getErrCode(), $deliveryOrderService->getErrMsg());
    }
    public function actionDeliveryDoneList()
    {
        $page=\Yii::$app->request->get("page",1);
        $page=max(1,$page);
        $pageSize=\Yii::$app->request->get("page-size",10);
        $type=\Yii::$app->request->get("type",0);
        $keyword=\Yii::$app->request->get("keyword",'');
        $startTime=\Yii::$app->request->get("delivery_start_time",0);
        $endTime=\Yii::$app->request->get("delivery_end_time",0);
        $timeQuantum=['startTime'=>$startTime,'endTime'=>$endTime];
        /* @var DeliveryOrderService $deliveryOrderService */
        $deliveryOrderService=\Yii::createObject(DeliveryOrderService::className());
        $data=$deliveryOrderService->getDeliveryDoneList($page,$pageSize,$type,$keyword,$timeQuantum);
        return $this->exportJson($data, $deliveryOrderService->getErrCode(), $deliveryOrderService->getErrMsg());
    }
}